Ext.define('channel.LoginWindow',{
	extend:'Ext.window.Window',
	banner:Ext.create('Ext.Component',{
		region:'north',
		height:100,
		margins:'1 1 1 1',
		autoEl:{
			tag:'div',
			style:'background-image:url(/style/common/images/loginimg.jpg)'
		}
	}),
	form:Ext.create('Ext.form.Panel',{
		region:'center',
		border:false,
		bodyPadding:20,
		defaults:{selectOnFocus:true,msgTarget:'side',xtype:'textfield',maxLength:45,allowBlank:false,labelAlign:'right'},
		items:[{
			fieldLabel:'登录名',
			name:'username',
			fieldCls:'loginUser',
			anchor:'90%'
		},{
			fieldLabel:'密码',
			name:'password',
			inputType:'password',
			fieldCls:'loginKey',
			anchor:'90%'
		}]
	}),
	initComponent:function(){
		Ext.apply(this,{
			title:'Channel Web Manage',
			width:500,
			height:320,
			resizable:false,
			closable:false,
			layout:'border',
			items:[this.form,this.banner],
			buttonAlign:'right',
			buttons:[
		         {text:'登录',scope:this,handler:function(){this.login();}},
		         {text:'重置',scope:this,name:'reset',handler:function(){
		        	 this.down('form').getForm().reset();
		        	 this.down('form').getForm().findField('username').focus(true,true);
		         }},
		         ]
		});
		this.callParent();
		this.on('show',function(){
        	this.down('form').getForm().findField('username').focus(true,true);
        },this);
	},
	login:function(){
		if(this.down('form').getForm().isValid()){
			this.down('button').setDisabled(true);
			this.down('[name=reset]').setDisabled(true);
			var params = this.down('form').getForm().getValues();
			params.method = 'POST';
			channel.openLink({
				masker:this,
				url:'/member/login.json',
				params:params,
				scope:this,
				onSuccess:function(rs,opts){
					params = null;
					window.location.href='/view/main';
				},
				onFailure:function(rs,opts,btn){
					this.down('form').getForm().findField('username').focus(true,true);
					this.down('button').setDisabled(false);
					this.down('button').nextSibling('button').setDisabled(false);
				}
			});
		}
	}
});


var initLoginWindow = function(){
	var loginWindow = Ext.create('channel.LoginWindow');
	var b = Ext.get('login-loading');
	var a = Ext.get('loading-mask');
	a.setOpacity(0.9);
	a.shift({
	        x:b.getX(),
	        y:b.getY(),
	        width:b.getWidth(),
	        height:b.getHeight(),
	        remove:true,
	        duration:1,
	        opacity:0.3,
	        easing:"bounceOut",
	callback:function(){
	    b.fadeOut({duration:0.3,remove:true});
	}
	});
	loginWindow.show();
}
